package com.wlyuan.open.scanner.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wlyuan.open.scanner.dao.entity.BasicAreaDO;
import com.wlyuan.open.scanner.dao.entity.OrderAdditionTransportDO;
import com.wlyuan.open.scanner.dao.mapper.OrderAdditionTransportMapper;
import com.wlyuan.open.scanner.domain.order.OrderTransport;
import com.wlyuan.open.scanner.domain.order.OrderTransportConvert;
import com.wlyuan.open.scanner.repository.BasicAreaRepository;
import com.wlyuan.open.scanner.repository.OrderAdditionTransportRepository;
import com.wlyuan.open.scanner.utils.StreamUtils;
import com.wlyuan.open.scanner.utils.enums.InvalidEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @author lijing
 * @description
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class OrderAdditionTransportRepositoryImpl implements OrderAdditionTransportRepository {
    private final OrderAdditionTransportMapper orderAdditionTransportMapper;
    private final BasicAreaRepository basicAreaRepository;

    @Override
    public List<OrderTransport> getByOrderId(Long orderId) {
        return Optional.ofNullable(orderId).map(order -> {
            var list = select(order);
            return OrderTransportConvert.convert(list);
        }).orElse(null);
    }

    @Override
    public OrderTransport getById(Long additionTransportId) {
        return Optional.ofNullable(additionTransportId).map(id -> {
            var data = orderAdditionTransportMapper.selectById(id);
            return Optional.ofNullable(data).map(addition -> {
                processArea(Arrays.asList(addition));
                return OrderTransportConvert.convert(addition);
            }).orElse(null);
        }).orElse(null);
    }

    private List<OrderAdditionTransportDO> select(Long orderId) {
        return (List<OrderAdditionTransportDO>) Optional.ofNullable(orderId).map(id -> {
            LambdaQueryWrapper<OrderAdditionTransportDO> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(OrderAdditionTransportDO::getTransportOrderid, id).eq(OrderAdditionTransportDO::getTransportInvalid, InvalidEnum.ENABLE.getValue());
            List<OrderAdditionTransportDO> list = orderAdditionTransportMapper.selectList(queryWrapper);
            if (CollectionUtils.isEmpty(list)) {
                return Collections.emptyList();
            }
            processArea(list);
            return list;
        }).orElseGet(() -> Collections.emptyList());
    }

    private void processArea(List<OrderAdditionTransportDO> list) {
        //运输地址信息
        var areaMap = getArea(list);
        list.stream().forEach(data -> {
            data.setWayProvince(Optional.ofNullable(areaMap.get(data.getTransportWayprovince())).map(BasicAreaDO::getAreaName).orElse(""));
            data.setWayCity(Optional.ofNullable(areaMap.get(data.getTransportWaycity())).map(BasicAreaDO::getAreaName).orElse(""));
            data.setWayDistrict(Optional.ofNullable(areaMap.get(data.getTransportWaydistrict())).map(BasicAreaDO::getAreaName).orElse(""));
        });
    }

    /**
     * 获取地区信息
     *
     * @param list
     * @return
     */
    private Map<Long, BasicAreaDO> getArea(List<OrderAdditionTransportDO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyMap();
        }
        var province = StreamUtils.mapList(list, OrderAdditionTransportDO::getTransportWayprovince);
        var city = StreamUtils.mapList(list, OrderAdditionTransportDO::getTransportWaycity);
        var distinct = StreamUtils.mapList(list, OrderAdditionTransportDO::getTransportWaydistrict);
        return basicAreaRepository.getAreaMap(province, city, distinct);

    }

}
